package com.znjc.iot.websocket;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.znjc.iot.domain.IotDataVO;
import com.znjc.iot.domain.IotTypeVO;
import com.znjc.iot.service.IotService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 自定义WebSocket客户端
 */
@Slf4j
public class CustomizedWebSocketClient extends WebSocketClient {

    IotService iotService;

    public IotService getIotService() {
        if(iotService == null){
            iotService = SpringUtil.getBean(IotService.class);
        }
        return iotService;
    }


    /**
     * 线程安全的Boolean -是否受到消息
     */
    public AtomicBoolean hasMessage = new AtomicBoolean(false);

    /**
     * 线程安全的Boolean -是否已经连接
     */
    private AtomicBoolean hasConnection = new AtomicBoolean(false);

    /**
     * 构造方法
     *
     * @param serverUri
     */
    public CustomizedWebSocketClient(URI serverUri) {
        super(serverUri);
        log.info("CustomizeWebSocketClient init:" + serverUri.toString());
    }

    /**
     * 打开连接是方法
     *
     * @param serverHandshake
     */
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info("CustomizeWebSocketClient onOpen");
    }

    /**
     * 收到消息时
     *
     * @param data
     */
    @Override
    public void onMessage(String data) {
        log.info("接受到的消息:" + data);
        hasMessage.set(true);
        if(StringUtils.isNotEmpty(data)){
            JSONObject jsonObject = JSONObject.parseObject(data);
            //启停采集
            if(jsonObject.containsKey("openFlag")){
                IotTypeVO typeVO = JSONObject.parseObject(data, IotTypeVO.class);
                getIotService().openFlag(typeVO);
            }else if(jsonObject.containsKey("commands")){
                //下发设置指令
                IotDataVO dataVO = JSONObject.parseObject(data,IotDataVO.class);
                getIotService().saveMasterData(dataVO);
            }else if(jsonObject.containsKey("closeCOM")){
                //关闭串口
                getIotService().closePort(jsonObject.getString("coms"));
            }else if(jsonObject.containsKey("taskCommand")){
                //定时命令
                getIotService().taskCommand(jsonObject.getString("command"));
                log.info(jsonObject.getString("taskCommand"));
            }
        }
    }

    /**
     * 当连接关闭时
     *
     * @param i
     * @param s
     * @param b
     */
    @Override
    public void onClose(int i, String s, boolean b) {
        this.hasConnection.set(false);
        this.hasMessage.set(false);
        log.info("CustomizeWebSocketClient onClose:" + s);
    }

    /**
     * 发生error时
     *
     * @param e
     */
    @Override
    public void onError(Exception e) {
        log.info("CustomizeWebSocketClient onError:" + e);
    }

    @Override
    public void connect() {
        if(!this.hasConnection.get()){
            super.connect();
            hasConnection.set(true);
        }
    }
}


